package com.fingard.dsp.task.command.receipt;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.fingard.FGBiz;
import com.fingard.client.DspClient;
import com.fingard.constant.Format;
import com.fingard.diagnostics.LogHelper;
import com.fingard.dsp.bank.dbobj.receipt.ReceiptData;
import com.fingard.dsp.bank.dbobj.receipt.ReceiptDataHelper;
import com.fingard.dsp.bank.directConfig.FileSaveSet;
import com.fingard.dsp.bank.helpers.FileType;
import com.fingard.dsp.bank.sepro.ReceiptPro;
import com.fingard.dsp.interact.FileServerInvokerBase;
import com.fingard.dsp.interact.IFileServerInvoker;
import com.fingard.dsp.interact.UploadFileReq;
import com.fingard.dsp.interact.UploadFileResp;
import com.fingard.dsp.task.DelayCounter;
import com.fingard.dsp.task.ITask;
import com.fingard.dsp.task.command.SyncReceipt;
import com.fingard.generalobj.ExecResponse;
import com.fingard.io.FileHelper;
import com.fingard.net.ConnState;
import com.fingard.net.FormSubmit;
import com.fingard.net.FtpReceiver;
import com.fingard.net.MixFtp;
import com.fingard.net.TcpReceiver;
import com.fingard.text.StringHelper;
import com.fingard.util.DateHelper;
import com.fingard.util.SmbFileUtil;
import com.fingard.xml.XmlTextReader;

/**上传回单文件*/
public class UploadReceiptFile extends SyncReceipt implements ITask {
	//private ReceiptProConfig proConfig = null;
	
	//文件上传delayCounter
	private static DelayCounter delayCounterUpload = new DelayCounter(100, 2);//最大延时100分钟，按平方计算，即4次=4*4=16分钟
	public static void resetDelayCounter(){
		delayCounterUpload.resetZeroCount();
	}
	
	public UploadReceiptFile(){
		logCode = "task"+File.separator+"receipt"+File.separator+"UploadReceiptFile";
	}
	
	public UploadReceiptFile(String pLogCode){
		logCode = pLogCode;
	}
	
	private static long polledCount = 0;//轮询次数
	public long getPolledCount(){
		return polledCount;
	}
	
	private static Date lastPolledTime;//最近轮询时间
	public Date getLastPolledTime(){
		return lastPolledTime;
	}
	
	/**pParams的key已转小写
	 *          <paramList>
                    <bank></bank>
                    <beginDayCount>30</beginDayCount>
                    <endDayCount>0</endDayCount>
                </paramList>
           */
	@Override
	public void execTask(HashMap<String, String> pParams) {
		try{
			lastPolledTime = new Date();
			boolean isDisposable = false;//当前是否一次性打印，即第一次需要打印
			long tmpDisposableCount = polledCount;
			polledCount++;
			if(tmpDisposableCount == 0){
				isDisposable = true;
			}
			proConfig = ReceiptPro.getReceiptProConfig();
			
			int[] days = getBeginEndDay(pParams);
			int beginDayCount = days[0];
			int endDayCount = days[1];
    		
    		//文件上传
    		if(proConfig.fileSetList.size()>0){
    			if(polledCount % 20 == 1){
    				FGBiz.logTrace.debug(logCode,logID + "\t开始执行电子回单文件同步"+polledCount);
    			}
    			if(isDisposable){
    				StringBuilder sbLog = new StringBuilder();
					for(FileSaveSet eachSaveSet : proConfig.fileSetList){
						sbLog.append("id:"+eachSaveSet.id+",saveType:"+eachSaveSet.saveType+",server:"+eachSaveSet.getServerAddrForLog()+"\r\n");
					}
					FGBiz.logTrace.info(logCode,sbLog.toString());
    			}
    			/*execUpload遍历日期，调用uploadFileByData，
        		 * uploadFileByData方法中根据上传方式类型，分别调用uploadByMixFtp获uploadByShareDir等
        		 * */
				execUpload(beginDayCount, endDayCount);
				if(polledCount % 20 == 1){
					FGBiz.logTrace.info(logCode, logID + "\t本次文件上传运行结束"+polledCount);
				}
			}else{
				if(isDisposable){
					FGBiz.logTrace.info(logCode,logID + "\t无开启的fileSaveSet配置节点");
				}
			}
		}catch(Throwable t){
			FGBiz.logTrace.exception(logCode, t);
		}finally{
			
		}
	}
	
	/**执行文件上传*/
	private void execUpload(int pBeginDayCount, int pEndDayCount){
		if(delayCounterUpload.isDelay()){
			return;
		}
		int totalNeedUp = 0;
		if(proConfig.fileSetList.size()>0){
    		//文件上传
			Date nowDay = new Date();
			ArrayList<String> tenantList = FGBiz.dspCenter.getDistinctTenant();
			for(String eachTenant : tenantList){//遍历所有租户
				for(int dCount=pBeginDayCount; dCount<=pEndDayCount; dCount++){
	    			Date eachDay = DateHelper.addDays(nowDay, dCount);
	    			ReceiptDataHelper receiptHelper = null;
	    			try{
	    				receiptHelper = new ReceiptDataHelper(eachTenant, eachDay);
	            		int maxCount = ReceiptDataHelper.getMaxTabCount();
	            		for(int idx=0;idx<maxCount;idx++){//dsp做了分表处理，需遍历每个表
	            			String logPre = logID +"\t"+ Format.Date10Format.format(eachDay)+"表"+idx;
	            			ArrayList<Long> uridList = receiptHelper.getNeedUpList(idx, eachDay);
	            			if(uridList != null && uridList.size() > 0){
	            				totalNeedUp += uridList.size();
	            				FGBiz.logTrace.info(logCode,logPre+"，需上传"+uridList.size()+"条");
	            				//执行上传
	            				try{
	            					int succCount = uploadFileByData(receiptHelper, uridList, idx);
	            					FGBiz.logTrace.info(logCode,logPre+"，成功上传"+succCount+"条");
	            				}catch(Exception e){
	            					FGBiz.logTrace.exception(logCode,e,logPre);
	            				}
	            			}else{
	            				int rdCount = receiptHelper.getUpedCount(idx, eachDay);
	            				int c2 = receiptHelper.getDownedCount(idx, eachDay);
	            				int c3 = receiptHelper.getTotalCount(idx, eachDay);
	            				if(rdCount>0||c2>0||c3>0){
	            					FGBiz.logTrace.info(logCode,logPre+"，无需要上传的数据，已上传成功"+rdCount+"条，已下载"+c2+"条，总数"+c3+"条");
	            				}
	            			}
	            		}
	    			}catch(Exception ex){
	    				FGBiz.logTrace.exception(logCode, ex);
	    			}finally{
	    				if(receiptHelper!=null){
	    					receiptHelper.close();
	    				}
	    			}
	    		}
			}
    	}
		if(totalNeedUp == 0){
			delayCounterUpload.increaseZeroCount();
			FGBiz.logTrace.info(logCode,logID + "\t需上传条数为0，增加轮询延时"+delayCounterUpload.getDelayMinutes()+"分钟");
		}else{
			delayCounterUpload.resetZeroCount();
			PushReceiptData.resetDelayCounter();//同时重置数据推送延时计数
		}
	}
	

	/**根据数据上传文件*/
	private int uploadFileByData(ReceiptDataHelper pHelper, ArrayList<Long> pList, int pIdx) throws IllegalArgumentException, IllegalAccessException, SQLException, ParseException{
		int sCount = 0;
		MixFtp mixFtp = null;
		DspClient dspClient = null;
		IFileServerInvoker iFsInvoker = null;
		StringBuilder sbInitFsInvoker = new StringBuilder();//初始化IFileServerInvoker的日志
		try{
			int delCount = 0;
			for(int i=0;i<pList.size();i++){
				long urid = pList.get(i);
				ReceiptData aData = pHelper.loadFromDbByUrid(pIdx, urid);
				if(aData == null){
					FGBiz.logTrace.error(logCode,"tabidx:"+pIdx+",urid:"+urid+",loadFromDb:null");
					continue;
				}
				aData.upTime = new Date();
				//上传成功次数，当次数与要上传的系统数一致，才认为是上传成功
				int upSuccCount = 0;
				String localFilePath = proConfig.localSaveMainDir+File.separator+StringHelper.trimStartFileSp(aData.localFile);
				localFilePath = StringHelper.replaceFileSp(localFilePath);
				try{
					if(StringHelper.isNullOrEmpty(aData.localFile) || !FileHelper.existFile(localFilePath)){
						FGBiz.logTrace.error(logCode,"tabidx:"+pIdx+",urid:"+urid+",receiptNo:"+aData.receiptNo+"，本地文件不存在："+localFilePath);
						continue;
					}
					for(FileSaveSet eachSaveSet : proConfig.fileSetList){
						//调用不同保存方法
						/*!!注意!!：
						 * 被调方法中要赋值upInfo，
						 * 上传成功成功要赋值upFile，
						 * resp.isSuccess表示上传成功*/
						ExecResponse resp = null;
						if("sftp".equalsIgnoreCase(eachSaveSet.saveType) || "ftp".equalsIgnoreCase(eachSaveSet.saveType)){
							//sftp/ftp保存方式
							if(eachSaveSet.ftpSet == null){
								FGBiz.logTrace.error(logCode, "id为"+eachSaveSet.id+"的文件保存设置，未配置<ftpSet>");
								return 0;
							}
							//调用uploadByMixFtp进行上传
							if(mixFtp == null){
								if(StringHelper.isNullOrEmpty(eachSaveSet.ftpSet.ftpType)){
									eachSaveSet.ftpSet.ftpType = eachSaveSet.saveType;
								}
								FGBiz.logTrace.info(logCode,eachSaveSet.ftpSet.ftpType + " "+eachSaveSet.ftpSet.serverIP+":"+eachSaveSet.ftpSet.port);
								mixFtp = new MixFtp(eachSaveSet.ftpSet);
								mixFtp.connect();
							}
							resp = uploadByMixFtp(mixFtp, aData, localFilePath);
							
						}else if("shareDir".equalsIgnoreCase(eachSaveSet.saveType)){
							//调用文件目录方式保存或上传
							resp = uploadByShareDir(eachSaveSet.shareDir, aData, localFilePath, eachSaveSet.getCharset("gbk"));
						}else if("copyToDir".equalsIgnoreCase(eachSaveSet.saveType)){
							//复制到指定目录
							resp = uploadByCopyToDir(eachSaveSet, aData, localFilePath);
						}else if("remoteHost".equalsIgnoreCase(eachSaveSet.saveType)){
							//发送至远程dsp
							if(dspClient == null){
								dspClient = new DspClient(eachSaveSet.mainHostServer);
								String charset = eachSaveSet.getCharset(FGBiz.dspCenter.dspSet.encodingWithDspClient);
								dspClient.setCharset(charset);
								FGBiz.logTrace.info(logCode, "远程服务地址："+eachSaveSet.mainHostServer+"，交互字符集："+charset);
							}
							resp = uploadBySocket(dspClient, aData, localFilePath);
						}else if("custom.fileserver".equalsIgnoreCase(eachSaveSet.saveType) || "oss".equalsIgnoreCase(eachSaveSet.saveType)){
							if(iFsInvoker == null){
								iFsInvoker = getFsInvoker(eachSaveSet, sbInitFsInvoker);
								if(iFsInvoker!=null){
									iFsInvoker.setLogCode(logCode);
								}
							}
							if(iFsInvoker == null){
								aData.upInfo = sbInitFsInvoker.toString();
								resp = new ExecResponse();
								resp.logMsg = sbInitFsInvoker.toString();
								break;
							}else{
								resp = uploadByFileServer(iFsInvoker, eachSaveSet, aData, localFilePath);
							}
						}else{
							resp = new ExecResponse();
							aData.upInfo = "不支持的保存方式："+eachSaveSet.saveType;
							break;
						}
						if(resp.isSucceed){
							upSuccCount++;
						}else{
							FGBiz.logTrace.error(logCode, aData.srcSys+"，urid为" + aData.urid+"，回单号为"+aData.receiptNo+"，上传失败："+aData.upInfo);
						}
					}
				}catch(Exception ex){
					aData.upInfo = LogHelper.getStackTrace(ex);
					FGBiz.logTrace.exception(logCode, ex);
				}
				
				if(upSuccCount == proConfig.fileSetList.size()){
					//更新数据库
					sCount++;
					aData.upState = "2";
				}else{
					aData.upState = "3";
					if(upSuccCount>0){
						FGBiz.logTrace.info(logCode, "urid:" + urid + ", receiptNo:" + aData.receiptNo + ", 需推送"+proConfig.fileSetList.size()+"个服务器，成功推送"+upSuccCount+"个");	
					}
				}
				int rowsAff = pHelper.updateUpState(pIdx,aData);
				if(rowsAff != 1){
					FGBiz.logTrace.info(logCode, "urid:" + urid + ", receiptNo:" + aData.receiptNo + ", 上传状态"+aData.upState+"，更新影响行数：" + rowsAff);
				}else if(this.proConfig.deleteAfterUploaded){
					if("2".equals(aData.upState) && rowsAff > 0){
						File delFile = new File(localFilePath);
						if(delFile.delete()){
							delCount++;
						}
					}
				}
			}
			FGBiz.logTrace.info(logCode, "删除"+delCount+"个回单文件");
		}catch(Exception e){
			FGBiz.logTrace.exception(logCode, e);
		}finally{
			if (mixFtp != null) {
                try {
                	mixFtp.disconnect();
                } catch(IOException ex) {
                	FGBiz.logTrace.exception(logCode, ex);
                }
            }
			if(dspClient !=null){
				dspClient = null;
			}
			if(iFsInvoker != null){
				iFsInvoker = null;
			}
		}
		return sCount;
	}
	
	/**文件目录方式，需赋值upInfo，含企金的上传至微服务，格式:Http://ip:port/..
	 * @throws Exception */
	private ExecResponse uploadByShareDir(String pShareDir, ReceiptData pData, String pLocalFilePath, String pCharset) throws Exception{
		ExecResponse resp = new ExecResponse();
		String uploadType = getUploadTypeByPath(pShareDir);
		
		String busDate = pData.busDate+"";

		String subDirByDate = busDate.substring(0, 6) + "/" + busDate.substring(6, 8) + "/" + pData.srcSys + "/" + pData.ownAct + "/";

		String ext = StringHelper.getFileExt(pLocalFilePath);
		String upFileName = pData.receiptNo+"."+ext;

		int result = -1;
		if("smb".equalsIgnoreCase(uploadType)) {
			String shareDirPath = pShareDir.trim();
			if(!shareDirPath.endsWith("/") && !shareDirPath.endsWith("\\")){
				shareDirPath+="/";
			}
            result = SmbFileUtil.smbUploadingInChar(pLocalFilePath, subDirByDate, shareDirPath, upFileName);
            pData.upFile = subDirByDate + upFileName;
        }else if("localhost".equalsIgnoreCase(uploadType)){
        	String shareDir = StringHelper.trimEndFileSp(pShareDir.split("@")[1]) + File.separator;
        	shareDir = StringHelper.replaceFileSp(shareDir);
        	subDirByDate = StringHelper.replaceFileSp(subDirByDate);
        	
        	pData.upFile = subDirByDate + upFileName;
        	result = FileHelper.copyFile(pLocalFilePath, shareDir + subDirByDate + upFileName);
        }else{
        	subDirByDate = subDirByDate.replaceAll(pData.srcSys + "/","");
			pShareDir = pShareDir.replaceAll("\\\\","/");
			upFileName = pData.srcFile;
            Map<String,String> map = FormSubmit.fileUpload(pShareDir,pData.ownAct, pLocalFilePath, subDirByDate, upFileName, pCharset);
            FGBiz.limitMsg.add("FormSubmit.fileUpload", JSON.toJSONString(map));
            if(map.keySet().size()!=0){
                result = Integer.valueOf(map.get("respCode"));
                pData.upFile = map.get("remotePath");
                if(map.containsKey("respInfo")){
                	pData.upInfo = map.get("respInfo");
                }
            }
        }
		
		if(result >= 0){
        	resp.isSucceed = true;
        	pData.upInfo = "保存至"+uploadType+"目录成功";
        }else if(StringHelper.isNullOrEmpty(pData.upInfo)){
        	pData.upInfo = "保存至"+uploadType+"目录失败，返回值："+result;
        }
		
		return resp;
	}
	
	/**文件目录方式，根据地址识别种类*/
	private String getUploadTypeByPath(String pShareDir){
		String shareDir = pShareDir.trim().toLowerCase();
		if(shareDir.startsWith("smb:")){
            return "smb";
        }else if(shareDir.startsWith("localhost")){
            return "localhost";
        }else{
            return "microService";
        }
	}
	
	
	/**ftp或sftp上传文件，需赋值upInfo
	 * @throws Exception */
	private ExecResponse uploadByMixFtp(MixFtp pMixFtp, ReceiptData pData, String pLocalFilePath) throws Exception{
		
		ExecResponse resp = new ExecResponse();
		
		String upDir = pMixFtp.ftpSet.upDir;
		upDir = replaceDestDirFormat(upDir, pData);
		upDir = upDir.replace("\\", "/");
		String ext = StringHelper.getFileExt(pLocalFilePath);
		String upFileName = pData.receiptNo+"."+ext;
		FtpReceiver ftpReceiver = pMixFtp.uploadFileToDir(pLocalFilePath, upDir, upFileName);
		pData.upFile = StringHelper.trimEnd(upDir, "/")+"/"+upFileName;
		if(ftpReceiver.isCompleted){
			FGBiz.logTrace.info(logCode, "urid:" + pData.urid + "," + upFileName +"推送至"+pMixFtp.ftpSet.ftpType+"成功");
			resp.isSucceed = true;
			pData.upInfo = "推送至"+pMixFtp.ftpSet.ftpType+"成功";
		}else{
			boolean isExist = pMixFtp.existFile(upFileName);
			if(isExist){
				resp.isSucceed = true;
				pData.upInfo = pMixFtp.ftpSet.ftpType+"服务器上已存在";
			}else{
				pData.upInfo = ftpReceiver.connMsg;
				FGBiz.logTrace.info(logCode, "urid:" + pData.urid + "," + upFileName +"推送至"+pMixFtp.ftpSet.ftpType+"失败："+ftpReceiver.connMsg);
			}
		}
		
		return resp;
	}
	
	/**复制到指定目录，需赋值upInfo
	 * @throws Exception */
	private ExecResponse uploadByCopyToDir(FileSaveSet pSet, ReceiptData pData, String pLocalFilePath) throws Exception {
		
		ExecResponse resp = new ExecResponse();
		
		String subFilePath = replaceDestFilePathFormat(pSet.filePathFormat, pData);
		String destFilePath = pSet.getMainDir(pData.bank);
		destFilePath = StringHelper.trimEndFileSp(destFilePath)+File.separator+StringHelper.trimStartFileSp(subFilePath);
		resp.isSucceed = FileHelper.copyFileWithCompare(new File(pLocalFilePath), destFilePath);
		if(resp.isSucceed){
			pData.upFile = subFilePath;
			pData.upInfo = "复制成功";
		}else{
			pData.upInfo = "复制失败，目的文件："+destFilePath;
		}
		return resp;
	}
	
	/**发送到远程DSP，需赋值upInfo
	 * @throws UnsupportedEncodingException */
	private ExecResponse uploadBySocket(DspClient pDspClient, ReceiptData pData, String pLocalFilePath) throws UnsupportedEncodingException{
		
		ExecResponse resp = new ExecResponse();
		TcpReceiver retReceiver = pDspClient.syncFile(pLocalFilePath, FileType.receipt, pData.srcSys, pData.bank, pData.localFile);
		pData.upFile = pData.localFile;
		if(retReceiver.isCompleted){
			String retXml = new String(retReceiver.recBytes, retReceiver.contentStartIndex,retReceiver.contentLength, retReceiver.charsetName);
			try{
				if(retXml.contains("<") && retXml.contains(">")){
					XmlTextReader retXmlRd = new XmlTextReader(retXml);
					if("0".equals(retXmlRd.getSimpleText("RespCode"))){
						resp.isSucceed = true;
						pData.upInfo = retXmlRd.getSimpleText("RespInfo");
					}else{
						pData.upInfo = retXmlRd.getSimpleText("RespInfo");
						FGBiz.limitMsg.addNoConsole(logCode, retXml);
					}
				}else{
					pData.upInfo = retXml;
				}
			}catch(Exception ex){
				FGBiz.logTrace.exception(logCode, ex, pData.localFile+"推送响应报文："+retXml);
				pData.upInfo = retXml + LogHelper.getStackTrace(ex);
			}
			
		}else{
			resp.logMsg = retReceiver.getConnMsg();
			pData.upInfo = retReceiver.getConnMsg();
		}
		return resp;
	}
	
	/**获取IFileServerInvoker：客制化文件服务器或云OSS*/
	private IFileServerInvoker getFsInvoker(FileSaveSet pSaveSet, StringBuilder pSb){
		IFileServerInvoker retFsInvoker = null;
		try {
            String className = "";
            if("custom.fileserver".equalsIgnoreCase(pSaveSet.saveType)){
            	FGBiz.logTrace.info(logCode, pSaveSet.saveType+"\tcustomCode:" + pSaveSet.customCode);
            	if(StringHelper.hasAnyChar(pSaveSet.customCode)){
            		className = "com.fingard.dsp.interact.custom." + pSaveSet.customCode.toLowerCase() + "." + "FileServerInvoker";
            	}else{
            		pSb.append("customCode设置为空");
            		return retFsInvoker;
            	}
            }else{
            	if(pSaveSet.ossSet != null){
            		if(StringHelper.hasAnyChar(pSaveSet.ossSet.ossType)){
            			FGBiz.logTrace.info(logCode, pSaveSet.saveType+"\tossType:" + pSaveSet.ossSet.ossType.toLowerCase());
            			className = "com.fingard.dsp.interact.oss." + pSaveSet.ossSet.ossType.toLowerCase() + "." + "FileServerInvoker";
            		}else{
            			pSb.append("ossSet.ossType设置为空");
                		return retFsInvoker;
            		}
            	}else{
            		pSb.append("ossSet设置为空");
            		return retFsInvoker;
            	}
            }
            Class<?> fsClass = Class.forName(className);
            if (fsClass != null) {
            	FileServerInvokerBase fsClassBase = (FileServerInvokerBase) fsClass.newInstance();
            	fsClassBase.fsSet = pSaveSet;
            	if("custom.fileserver".equalsIgnoreCase(pSaveSet.saveType)){
            		FGBiz.logTrace.info(logCode, "uploadUrl:" + pSaveSet.uploadUrl+", downloadUrl:" + pSaveSet.downloadUrl+", customServerUrl:"+pSaveSet.customServerUrl);
                	//resp.appendMsgLn("username:" + pSaveSet.username);
                	//resp.appendMsgLn("systemName:" + pSaveSet.systemName);	
            	}else{
            		FGBiz.logTrace.info(logCode, "bucket:" + pSaveSet.ossSet.bucket+", endpoint:" + pSaveSet.ossSet.endpoint);
            		//resp.appendMsgLn("accessKeyId:" + fileSet.ossSet.accessKeyId);
            		//resp.appendMsgLn("accessKeySecret:" + fileSet.ossSet.accessKeySecret);
            	}
            	retFsInvoker = (IFileServerInvoker) fsClassBase;
            }else{
            	pSb.append("未能找到该文件上传实现类");
        		return retFsInvoker;
            }
        }catch (Throwable t) {
        	FGBiz.logTrace.exception(logCode, t);
        	pSb.append(ConnState.CLASS_NOT_FOUND+"-"+LogHelper.getStackTraceFirstLine(t));
        }
		return retFsInvoker;
	}
	
	/**保存至客制化文件服务器或云OSS*/
	private ExecResponse uploadByFileServer(IFileServerInvoker pFsInvoker, FileSaveSet pSaveSet, ReceiptData pData, String pLocalFilePath){
		ExecResponse resp = new ExecResponse();
		
		UploadFileReq upReq = new UploadFileReq();
		upReq.upFileName = pData.ownAct+"_"+pData.receiptNo+"."+StringHelper.getFileExt(pData.localFile);
		upReq.upFileLocalPath = pLocalFilePath;
		UploadFileResp upResp = pFsInvoker.uploadFile(upReq);
		if(upResp.isCompleted){
			resp.isSucceed = true;
			pData.upInfo = "上传成功，rtnFileId："+upResp.rtnFileId;
			pData.upFile = upResp.rtnFileId;
		}else{
			pData.upInfo = StringHelper.joinNotEmpty("-", upResp.connState, upResp.sbMsg.toString());
		}
		resp.logMsg = pData.upInfo;
		return resp;
	}
}
